package com.yeyks.appcontroller;

import com.alibaba.fastjson.JSONObject;
import com.yeyks.base.Arguments;
import com.yeyks.constant.ConfigKey;
import com.yeyks.dal.domain.UserInfo;
import com.yeyks.dto.*;
import com.yeyks.exception.ServiceException;
import com.yeyks.model.Ajax.AjaxResponseBody;
import com.yeyks.service.*;
import com.yeyks.util.TextValidator;
import com.yeyks.util.Utility;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author fuchangshuai
 * @date 2019/4/25 0025
 */
@Api(tags = "推广接口", description = "推广")
@Slf4j
@RestController
@RequestMapping(value = "/app/recommend")
public class RecommendController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private RechargeRecordService rechargeService;
    @Autowired
    private CashOutOrderService cashOutOrderService;

    /**
     * h5用户充值
     *
     * @param token    token
     * @param openId   openId
     * @param price    充值金额
     * @param inviteId 邀请码
     * @param type     类型(gzh,xcx)默认gzh
     * @param request
     * @return
     */
    @ApiOperation(value = "h5用户充值", notes = "当获取不到openid时code必传")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "openId", value = "openId", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "price", value = "充值金额", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "inviteId", value = "邀请码", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "类型(gzh,xcx)默认gzh", defaultValue = "gzh", dataType = "String")
    })
    @RequestMapping(value = "/h5/Pay", method = RequestMethod.POST)
    public ResponseEntity<?> h5Pay(@RequestParam(name = "token") String token,
                                   @RequestParam(name = "openId") String openId,
                                   @RequestParam(name = "price") Integer price,
                                   @RequestParam(name = "inviteId", required = false) String inviteId,
                                   @RequestParam(name = "type", defaultValue = "gzh") String type,
                                   HttpServletRequest request) {
        UserInfo userInfo = userInfoService.tokenUserInfo(token);
        if (rechargeService.isRecharge(userInfo.getId())) {
            throw new ServiceException("您已参与过本次活动");
        }
        if (Arguments.notEmpty(inviteId)) {
            if (userInfo.getInviteCode() != null && inviteId.equals(userInfo.getInviteCode())) {
                throw new ServiceException("不能为本人邀请码");
            }
        }
        PayParam payParam = new PayParam(userInfo.getId(), null, inviteId, price, type, Utility.getRemoteIP(request), openId);
        Map<String, Object> map = rechargeService.h5Pay(payParam);
        return ResponseEntity.ok(AjaxResponseBody.success(map));
    }

    /**
     * app用户推广充值
     *
     * @param token    token
     * @param price    充值金额
     * @param inviteId 邀请码
     * @param type     类型(wxpay,alipay)alipay
     * @param request
     * @return
     */
    @ApiOperation(value = "app用户推广充值", notes = "type默认alipay")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "price", value = "充值金额", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "inviteId", value = "邀请码", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "类型(wxpay,alipay)默认alipay", defaultValue = "alipay", dataType = "String")
    })
    @RequestMapping(value = "/app/Pay", method = RequestMethod.POST)
    public ResponseEntity<?> appPay(@RequestParam(name = "token") String token,
                                    @RequestParam(name = "price") Integer price,
                                    @RequestParam(name = "inviteId", required = false) String inviteId,
                                    @RequestParam(name = "type", defaultValue = "alipay") String type,
                                    HttpServletRequest request) {
        UserInfo userInfo = userInfoService.tokenUserInfo(token);
        if (rechargeService.isRecharge(userInfo.getId())) {
            throw new ServiceException("您已参与过本次活动");
        }
        if (Arguments.notEmpty(inviteId)) {
            if (userInfo.getInviteCode() != null && inviteId.equals(userInfo.getInviteCode())) {
                throw new ServiceException("不能为本人邀请码");
            }
        }
        PayParam payParam = new PayParam(userInfo.getId(), null, inviteId, price, type, Utility.getRemoteIP(request), null);
        Map<String, Object> map = rechargeService.appPay(payParam);
        return ResponseEntity.ok(AjaxResponseBody.success(map));
    }

    /**
     * 佣金明细
     *
     * @param token token
     * @param type  类型(21、活动通知下优惠券 22、活动通知下佣金  23、活动通知下活动 24、活动通知下钱包)
     * @return
     */
    @ApiOperation(value = "佣金明细", notes = "佣金明细", response = RecommendDetailDTO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "类型(22、活动通知下佣金)", defaultValue = "22", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "pageNo", value = "当前页", defaultValue = "1", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "页码", defaultValue = "10", dataType = "int")
    })
    @RequestMapping(value = "/detail/list", method = RequestMethod.POST)
    public ResponseEntity<?> h5VipPay(@RequestParam(name = "token") String token,
                                      @RequestParam(name = "type", defaultValue = "22") String type,
                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        UserInfo userInfo = userInfoService.tokenUserInfo(token);
        List<RecommendDetailDTO> recommendDetailDTOs = rechargeService.detailList(userInfo.getId(), type, pageNo, pageSize);
        return ResponseEntity.ok(AjaxResponseBody.success(recommendDetailDTOs));
    }

    /**
     * 跳转我的佣金
     *
     * @param token
     * @return
     */
    @ApiOperation(value = "跳转我的佣金", notes = "跳转我的佣金", response = AccountOverviewDTO.class)
    @ApiImplicitParam(paramType = "query", name = "token", value = "token", required = true, dataType = "String")
    @PostMapping(value = "/info")
    public ResponseEntity<?> info(@RequestParam(name = "token") String token) {
        UserInfo userInfo = userInfoService.tokenUserInfo(token);
        AccountOverviewDTO accountOverviewDTO = cashOutOrderService.info(userInfo);
        return ResponseEntity.ok(AjaxResponseBody.success(accountOverviewDTO));
    }

    /**
     * 推广规则
     *
     * @return
     */
    @ApiOperation(value = "推广规则", notes = "推广规则")
    @PostMapping(value = "/rule")
    public ResponseEntity<?> rule() {
        List<String> lists = rechargeService.rule();
        return ResponseEntity.ok(AjaxResponseBody.success(lists));
    }

    /**
     * 获取活动链接
     *
     * @return
     */
    @ApiOperation(value = "获取活动链接", notes = "获取活动链接")
    @RequestMapping(value = "/url", method = RequestMethod.POST)
    public ResponseEntity<?> url() {
        Object value = rechargeService.getUrl();
        return ResponseEntity.ok(AjaxResponseBody.success(value));
    }

    /**
     * 判断用户是否已经充值
     *
     * @return
     */
    @ApiOperation(value = "判断用户是否已经充值", notes = "根据body中返回true(已充值)和false(未充值)")
    @ApiImplicitParam(paramType = "query", name = "phone", value = "手机号", required = true, dataType = "String")
    @RequestMapping(value = "/is/recharge", method = RequestMethod.POST)
    public ResponseEntity<?> isRecharge(@RequestParam(name = "phone") String phone) {
        checkParamPhone(phone);
        UserInfo userInfo = userInfoService.selectUseInfoPhone(phone);
        return ResponseEntity.ok(AjaxResponseBody.success(rechargeService.isRecharge(userInfo.getId())));
    }

    private void checkParamPhone(String phone) {
        if (Arguments.isEmpty(phone)) {
            throw new ServiceException("手机号不能为空");
        }
        if (!TextValidator.MOBILE.boolCheck(phone)) {
            throw new ServiceException("手机号格式非法");
        }
    }

}
